package ui.activity.exchange;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import business.service.exchange.BDLocationService;

import com.lab107team.R;


public class LocateActivity extends Activity implements OnClickListener, Callback {

	private static final String TAG = LocateActivity.class.getSimpleName();

	private BDLocationService service;

	private TextView tvContent;
	private Button btnStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.locate_activity_layout);
		
		initControlsAndRegEvent();
	}

	protected void initControlsAndRegEvent() {
		Log.d(TAG, "Initialize LocateActivity");

		service = new BDLocationService(getApplicationContext(), new Handler(this));

		btnStart = (Button) findViewById(R.id.btn_locate_start);
		tvContent = (TextView) findViewById(R.id.tv_locate_content);
		
		btnStart.setOnClickListener(this);
	}

	@Override
	public boolean handleMessage(Message msg) {
		Log.d(TAG, "Receive Message: " + msg);
		
		switch (msg.what) {
		case BDLocationService.LOCATE_SUCCESS:
			tvContent.setText((CharSequence) msg.obj);
			btnStart.setText("Start");
			service.stop();
			return true;

		case BDLocationService.LOCATE_FAIL:
			tvContent.setText((CharSequence) msg.obj);
			btnStart.setText("Start");
			service.stop();
			return true;
		}

		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_locate_start:
			onClickStartButton();
			break;

		default:
			break;
		}
	}

	private void onClickStartButton() {
		Log.d(TAG, "Start Locating...");
		
		service.start();
		btnStart.setText("Locating");
	}

}
